/** 
 * 
 * @author geloin 
 * @date 2012-5-5 涓婂崍9:31:52 
 */
package com.wcs.spring.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wcs.spring.entity.Menu;
import com.wcs.spring.service.MenuService;

/**
 * @author geloin
 * @date 2012-5-5 涓婂崍9:31:52
 */
@Controller
@RequestMapping(value = "background")
public class DemoController {

	@Resource(name = "menuService")
	private MenuService menuService;

	/**
	 * @author geloin
	 * @date 2012-5-5 涓婂崍9:33:22
	 * @return
	 */
	@RequestMapping(value = "to_login")
	public ModelAndView toLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String str_pageNum = request.getParameter("pageNumber");
		String str_pageSize = "1";// request.getParameter("pageNumber");
		int pageNum = 1, pageSize = 1;
		if (null != str_pageNum) {
			try {
				pageNum = Integer.parseInt(str_pageNum);
			}
			catch (NumberFormatException e) {
				request.setAttribute("err", "页码只能是大于0的整数，请重新输入!");
				return new ModelAndView("background/menu", map);
			}
		}
		if (null != str_pageSize) {
			try {
				pageSize = Integer.parseInt(str_pageSize);
			}
			catch (NumberFormatException e) {
				request.setAttribute("err", "页面大小只能是大于0的整数，请重新输入!");
				return new ModelAndView("background/menu", map);
			}
		}
		// List<Menu> result = this.menuService.find();
		//
		// map.put("result", result);

		// int pageNum = 1, pageSize = 2;
		// SqlSession sqlSession = MybatisHelper.getSqlSession();
		// CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
		PageInfo<Menu> page = null;
		try {
			PageHelper.startPage(pageNum, pageSize);
			List<Menu> list = this.menuService.find();
			page = new PageInfo(list);
			request.setAttribute("page", page);
		}
		catch (Exception e) {
			request.setAttribute("err", "error:" + e.getMessage());
		}
		map.put("result", page);
		request.setAttribute("p", 1);

		return new ModelAndView("background/menu", map);
	}
}
